package view;

import java.io.*;
import javax.swing.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Objects;

public class Client extends JFrame {
    private JTextArea screen;
    private JTextField input;
    private JButton connection;
    private JButton help;

    private Socket socket;
    private BufferedReader in;
    private BufferedWriter out;

    private String ipaddress = "192.168.122.1";
    private int port = 1888;
    private boolean connected = false;

    class MonitorThread extends Thread {
        public MonitorThread(BufferedReader br) {
            this.br = br;
        }

        BufferedReader br;

        @Override
        public void run() {
            while (br != null) {
                try {
                    String line= br.readLine();
                    setText(screen,line);
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
    }

    public Client() {
        super("Mud view.Client");
        Container container = this.getContentPane();
        container.setLayout(new BorderLayout());
        JPanel leftPanel = new JPanel();
        JPanel rightPanel = new JPanel();
        container.add(BorderLayout.CENTER, leftPanel);
        container.add(BorderLayout.EAST, rightPanel);
        leftPanel.setLayout(new BorderLayout());
        rightPanel.setLayout(new BoxLayout(rightPanel,BoxLayout.Y_AXIS));
        screen = new JTextArea();
        screen.setEditable(false);
        screen.setAutoscrolls(true);
        JScrollPane jsp = new  JScrollPane(screen);
        input = new JTextField();
        connection = new JButton("connect");
        help = new JButton("   help   ");
        leftPanel.add(BorderLayout.CENTER, jsp);
        leftPanel.add(BorderLayout.SOUTH, input);
        // rightPanel.setLayout(new FlowLayout());
        rightPanel.add(connection);
        rightPanel.add(help);
        this.setSize(800, 600);
        this.setVisible(true);
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        connection.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {//下连接server
                if(!connected){
                    try {
                        socket = new Socket(ipaddress, port);
                        out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                        in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        connected = true;
                        MonitorThread mt = new MonitorThread(in);
                        mt.start();
                    } catch (Exception e) {
                        e.printStackTrace();
                        screen.setText(screen.getText() + "���ӷ�����ʧ�ܣ�������\n");
                        System.exit(1);
                    }
                }
                else {
                        setText(screen,"您已连接\n");
                }
                }

        });
        help.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if(connected){
                        String message = "l 或 look: 查看当前环境\n" +
                                "use:使用物品\n" +
                                "get:获取物品\n" +
                                "throw:丢弃物品\n"+
                                "quit: 退出游戏\n" +
                                "e 或 east: 向东移动\n" +
                                "w 或 west: 向西移动\n" +
                                "s 或 south: 向南移动\n" +
                                "n 或 north: 向北移动\n" +
                                "ne 或 northeast: 向东北移动\n" +
                                "nw 或 northwest: 向西北移动\n" +
                                "se 或 southeast: 向东南移动\n" +
                                "sw 或 southwest: 向西南移动\n" +
                                "u 或 up: 向上移动\n" +
                                "d 或 down: 向下移动\n" +
                                "attack或a:攻击\n"+
                                "help或h:帮助文档\n";
                        setText(screen,message);
                }else{
                    setText(screen,"请先登录！\n");
                }
            }
        });
        input.addKeyListener(new KeyAdapter() {
            public void keyTyped(KeyEvent arg0) {
                if (arg0.getKeyChar() == KeyEvent.VK_ENTER) {
                    try {
                        String message=input.getText();
                        out.write(message+"\n");
                        out.flush();
                        input.setText("");
                        if(Objects.equals(message, "quit")){
                            System.exit(0);
                        }
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }

            }
        });
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                shutdown();
                System.exit(0);
            }
        });
    }

    public void shutdown() {
        if (connected) {
            try {
                connected = false;
                if(out!=null){
                    out.write("quit"); // Send quit command to server
                    out.newLine();
                    out.flush();
                }
                if(socket != null){
                    socket.close();
                }
                if(in!=null){
                    in.close();
                }
                if(out!=null){
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public void setText(JTextArea screen, String Message) {
        String[] temp = Message.split("\t");
        for (int i = 0; i < temp.length; i++) {
            screen.setText(screen.getText() + temp[i] + "\n");
            // System.out.print(temp[i]+"\n");
        }
        screen.setCaretPosition(screen.getDocument().getLength());
    }

    public static void main(String[] args) {
        new Client();
    }
}
